#include "solar/trunk/NcomboBox.h"
#include "solar/trunk/NTitleBar.h"

NcomboBox::NcomboBox(QWidget *parent) : QWidget(parent),
    backups_mask(QColor(250,250,247)){
    vscroll->d_linear_subcolor({QColor(214,219,227)});
    vscroll->d_linear_addcolor({QColor(160,173,191)});
    vscroll->d_graph_color(QColor(140,155,176));
    vscroll->d_text_color(QColor(255,255,255));
    vscroll->setGraphTextFont(QFont("KaiTi_GB2312", 9));

    hscroll->d_linear_subcolor({QColor(214,219,227)});
    hscroll->d_linear_addcolor({QColor(160,173,191)});
    hscroll->d_graph_color(QColor(140,155,176));
    hscroll->d_text_color(QColor(255,255,255));
    hscroll->setGraphTextFont(QFont("KaiTi_GB2312", 9));

    bx_stall->setVerticalScrollBar(vscroll);
    bx_stall->setHorizontalScrollBar(hscroll);
    bx_stall->cornerWidget()->setVisible(false);

    setProperty("msrv-enter", false);
    setProperty("style-radius", 3);
    bx_lyout->addWidget(bx_lIco, 0, Qt::AlignVCenter);
    bx_lyout->addWidget(bx_line, 0, Qt::AlignVCenter);
    bx_lyout->addWidget(bx_rIco, 0, Qt::AlignVCenter);
    bx_lyout->setContentsMargins(1,1,1,1);
    bx_lyout->setSpacing(2);
    bx_stall->setWindowFlags(Qt::FramelessWindowHint);
    bx_line->setContentsMargins(0,0,0,0);
    bx_line->setClearButtonEnabled(true);
    bx_line->setStyleSheet(lineditStyle);
    bx_stall->setStyleSheet(listwidStyle);
    d_filler_on(NTitleBar::FillerType::BACKCOLOR, true);
    impact_int.insert("Orientation", NT::ORIEN_VERTICAL);
    connect(bx_lIco, &NinsipidView::msev_etr, this, &NcomboBox::ico_l_touch);
    connect(bx_lIco, &NinsipidView::msev_pre, this, &NcomboBox::msev_match_pre);
    connect(bx_rIco, &NinsipidView::msev_pre, this, &NcomboBox::ico_r_pree);
    connect(bx_rIco, &NinsipidView::msev_etr, this, &NcomboBox::ico_r_touch);
    connect(bx_stall, &NListWidget::msev_invisible, this, &NcomboBox::stall_invisible);
    connect(vscroll, &NScrollBar::slidervalue, this, &NcomboBox::tab_vscrollvalue_change);
    connect(hscroll, &NScrollBar::slidervalue, this, &NcomboBox::tab_hscrollvalue_change);
    connect(bx_stall, &NListWidget::currentTextChanged, this, [&](QString text){if (!text.isEmpty()) bx_line->setText(text);});
}

NcomboBox::~NcomboBox(){
    delete bx_stall;
}

void NcomboBox::d_radius(int radius){
    setProperty("style-radius", radius);
}

void NcomboBox::list_selected(){
    QList<QListWidgetItem*> sles = bx_stall->selectedItems();
    if (!sles.isEmpty())
    {
        QListWidgetItem *cur_item = sles.last();
        if (cur_item)
        {
            bx_line->setText(cur_item->text());
        }
    }
}

void NcomboBox::paintEvent(QPaintEvent *Event){
    QPainter captainPainter(this);
    QPainterPath path;
    captainPainter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
    QList<QColor> fillerList = impact_stcolor;
    QPixmap backGroundPixmap = impact_pixmap["BackGround"];
    QLinearGradient lineGrandient;
    double DetailAddtion = 0;
    double RelativeStep = 1.0 / (double)(fillerList.size() - 1);
    if (impact_int["Orientation"] == NT::ORIEN_HORIZONTAL)
    {
        lineGrandient = QLinearGradient(2, 0, width(), 0);
    }
    else
    {
        lineGrandient = QLinearGradient(0, 2, 0, height() - 2);
    }
    captainPainter.setPen(QPen(QColor(116,124,141), 1)); // 140,150,174
    captainPainter.setBrush(Qt::transparent);
    int radius = property("style-radius").toInt();
    path.addRoundedRect(captainPainter.window(), radius, radius);
    captainPainter.setClipPath(path);
    QMap<NTitleBar::FillerType, bool> drawInfo = impact_ftype;
    if (drawInfo[NTitleBar::FillerType::BACKCOLOR] && !fillerList.isEmpty())
    {
        foreach(QColor color, fillerList)
        {
            lineGrandient.setColorAt(DetailAddtion, color);
            lineGrandient.setSpread(QGradient::ReflectSpread);
            DetailAddtion += RelativeStep;
        }
        captainPainter.setBrush(lineGrandient);
    }
    else if (drawInfo[NTitleBar::FillerType::BACKPIXMAP] && !backGroundPixmap.isNull())
    {
        captainPainter.setBrush(backGroundPixmap);
    }
    else
    {
        captainPainter.setBrush(backups_mask);
    }
    captainPainter.drawPath(path);
    QWidget::paintEvent(Event);
}

void NcomboBox::resizeEvent(QResizeEvent *event){
    bx_lIco->setFixedHeight((height() / 4) * 3);
    bx_rIco->setFixedHeight((height() / 4) * 3);
    bx_line->setFixedHeight(height() - 2);
    update();
    QWidget::resizeEvent(event);
}

void NcomboBox::setVisible(bool vsb){
    QWidget::setVisible(vsb);
    update();
}

void NcomboBox::d_filler(QList<QColor> color){
    impact_stcolor = color;
}

void NcomboBox::d_filler(QColor color){
    impact_stcolor = {color};

}

void NcomboBox::d_filler(QString color){
    impact_stcolor = {QColor(color)};
}

void NcomboBox::d_background(QString curtain){
    impact_pixmap["BackGround"] = curtain;
}

void NcomboBox::d_filler_on(NTitleBar::FillerType Type, bool Enable){
    impact_ftype.insert(Type, Enable);
}

void NcomboBox::d_filler_del(NTitleBar::FillerType Type){
    if (Type == NTitleBar::FillerType::BACKCOLOR)
    {
        impact_stcolor.clear();
    }
    else if (Type == NTitleBar::FillerType::BACKPIXMAP)
    {
        impact_pixmap["BackGround"].clear();
    }
}

void NcomboBox::d_leftico_pix(QString sharp, QString lowkey){
    bx_lIco->doconfigur_pixmap(lowkey);
    bx_lIco->doconfigur_adjustedwidth(true);
    bx_lIco->doconfigur_imgsetting(NT::DRAWTYPE_ROUND, NT::PIXLOAD_IGNORERATIO_LOWER, 2);
    impact_pixmap["match-sharp"] = sharp;
    impact_pixmap["match-lowkey"] = lowkey;
}

void NcomboBox::d_rightico_pix(QString nor_lowkey, QString nor_sharp, QString unor_lowkey, QString unor_sharp){
    bx_rIco->doconfigur_pixmap(nor_lowkey);
    bx_rIco->doconfigur_adjustedwidth(true);
    bx_rIco->doconfigur_imgsetting(NT::DRAWTYPE_ROUND, NT::PIXLOAD_IGNORERATIO_LOWER, 2);
    impact_pixmap["dropdown-top-lowkey"] = nor_lowkey;
    impact_pixmap["dropdown-top-sharp"] = nor_sharp;
    impact_pixmap["dropdown-down-lowkey"] = unor_lowkey;
    impact_pixmap["dropdown-down-sharp"] = unor_sharp;

}

void NcomboBox::ico_r_pree(){//dropdown-cut 默认false朝上，点击切换down
    bx_stall->setGeometry(pos().x(), pos().y() + height(), width(), 100);
    if (impact_bool["dropdown-cut"])
    {
        bx_rIco->doconfigur_pixmap(impact_pixmap["dropdown-top-sharp"]);
        bx_stall->setVisible(false);
        impact_bool["dropdown-cut"] = false;
    }
    else
    {
        bx_rIco->doconfigur_pixmap(impact_pixmap["dropdown-down-sharp"]);
        extra_stall();
        bx_stall->setVisible(true);
        impact_bool["dropdown-cut"] = true;
    }
}

void NcomboBox::ico_r_keep(){
    bx_stall->setGeometry(pos().x(), pos().y() + height(), width(), 100);
    if (impact_bool["dropdown-cut"])
    {
        extra_stall();
        bx_stall->setVisible(true);
    }
    else
    {
        bx_rIco->doconfigur_pixmap(impact_pixmap["dropdown-down-sharp"]);
        extra_stall();
        bx_stall->setVisible(true);
        impact_bool["dropdown-cut"] = true;
    }
}

void NcomboBox::extra_stall(){
    int ed_w;
    int ed_h;
    QPoint mainpos;
    if (impact_pixmap["match-sharp"].isNull() && impact_pixmap["match-sharp"].isNull())
    {
        ed_w = width() - bx_rIco->width();
        ed_h = height();
        mainpos = mapToGlobal(QPoint(0,0));
    }
    else
    {
        ed_w = bx_line->width();
        ed_h = bx_line->height();
        mainpos = mapToGlobal(bx_line->pos());
    }
    if (ed_w < bx_stall->getMaxlenght().first)
    {
        if (bx_stall->getMaxlenght().first >= bx_line->width() * 2)
        {
            ed_w = bx_line->width() * 2;
        }
        else
        {
            ed_w = bx_stall->getMaxlenght().first + 20;
        }
    }
    QPoint realPoint(mainpos.x(), mainpos.y() + ed_h);
    bx_stall->setFixedWidth(ed_w);
    bx_stall->move(realPoint);
}

void NcomboBox::ico_r_touch(bool enter){ // dropdown-cut 为false朝上，为true朝下
    if (enter)
    {
        if (impact_bool["dropdown-cut"])
        {
            bx_rIco->doconfigur_pixmap(impact_pixmap["dropdown-down-sharp"]);
        }
        else
        {
            bx_rIco->doconfigur_pixmap(impact_pixmap["dropdown-top-sharp"]);
        }
    }
    else
    {
        if (impact_bool["dropdown-cut"])
        {
            bx_rIco->doconfigur_pixmap(impact_pixmap["dropdown-down-lowkey"]);
        }
        else
        {
            bx_rIco->doconfigur_pixmap(impact_pixmap["dropdown-top-lowkey"]);
        }
    }
}

void NcomboBox::ico_l_touch(bool enter){
    if (enter)
    {
        bx_lIco->doconfigur_pixmap(impact_pixmap["match-sharp"]);
    }
    else
    {
        bx_lIco->doconfigur_pixmap(impact_pixmap["match-lowkey"]);
    }
}

QListWidget * NcomboBox::g_stall(){
    return bx_stall;
}

void NcomboBox::d_font(QFont font){
    bx_line->setFont(font);
}

void NcomboBox::d_linecolor(QColor color){
    QString color_char = QList<QString>({QString::number(color.red()),
                                         QString::number(color.green()),
                                         QString::number(color.blue())}).join(",");
    bx_line->setStyleSheet(lineditStyle.replace("fore", color_char));
}

void NcomboBox::d_linebackground(QColor color){
    QString colorget = NULL;
    QList<QString> colorli = QList<QString>({QString::number(color.red()),
                                             QString::number(color.green()),
                                             QString::number(color.blue())});
    if (colorli.length() == 3)
    {
        colorget = QString("rgb(%1)").arg(colorli.join(","));
    }
    else if (colorli.length() == 4)
    {
        colorget = QString("rgba(%1)").arg(colorli.join(","));
    }
    bx_line->setStyleSheet(Extend::QssAttributeValueSwap(lineditStyle, {"QLineEdit"}, "background-color", colorget));
}

QLineEdit* NcomboBox::g_linedit(){
    return bx_line;
}

NinsipidView* NcomboBox::ico(NT::LOCALTION how){
    if (how == NT::LOCALTION_CENTER_LEFT)
    {
        return bx_lIco;
    }
    else if (how == NT::LOCALTION_CENTER_RIGHT)
    {
        return bx_rIco;
    }
    else
    {
        return bx_lIco;
    }
}

void NcomboBox::stall_invisible(){
    bool isenter = property("msrv-enter").toBool();
    if (!isenter) // 失去焦点时再运行代码，否则将造成冲突，使 ico_r_pree 失效
    {
        ico_r_pree();
        ico_r_touch(false);
    }
}

void NcomboBox::enterEvent(QEnterEvent *event){
    setProperty("msrv-enter", true);
    QWidget::enterEvent(event);
}

void NcomboBox::leaveEvent(QEvent *event){
    setProperty("msrv-enter", false);
    QWidget::leaveEvent(event);
}
